package com.ruoyi.system.service.impl;

import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.ruoyi.common.constant.StaticObj;
import com.ruoyi.system.factory.FaceEngineFactory;
import com.ruoyi.system.service.InitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.stereotype.Service;

/**
 * @author lzw
 * 程序初始化业务接口实现类
 */
@Slf4j
@Service
public class InitServiceImpl implements InitService {

    /**
     * 初始化人脸引擎配置
     */
    @Override
    public void initFaceEngineConfig() {
        FunctionConfiguration functionConfiguration = new FunctionConfiguration();
        //开启人脸、年龄、性别检测
        functionConfiguration.setSupportFaceDetect(true);
        functionConfiguration.setSupportFaceRecognition(true);
        functionConfiguration.setSupportAge(true);
        functionConfiguration.setSupportGender(true);
        functionConfiguration.setSupportFace3dAngle(true);
        //活体检测
        functionConfiguration.setSupportIRLiveness(false);
        functionConfiguration.setSupportLiveness(false);
        EngineConfiguration engineConfiguration = new EngineConfiguration();
        engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
        engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
        engineConfiguration.setFunctionConfiguration(functionConfiguration);
        engineConfiguration.setDetectFaceMaxNum(10);
        engineConfiguration.setDetectFaceScaleVal(16);
        StaticObj.DEFAULT_ENGINE_CONFIG = engineConfiguration;
    }

    /**
     * 初始化人脸引擎通用对象池
     */
    @Override
    public void initFaceEnginePool() {
        GenericObjectPoolConfig<FaceEngine> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setMinIdle(10);
        poolConfig.setMaxIdle(20);
        poolConfig.setMaxTotal(20);
        poolConfig.setLifo(false);
        StaticObj.FACE_ENGINE_POOL = new GenericObjectPool<>(new FaceEngineFactory(), poolConfig);
    }

    /**
     * 启动时获取一次引擎对象，触发人脸引擎的活
     */
    @Override
    public void triggerEngineActivation() throws Exception {
        //获取一次人脸引擎触发激活
        FaceEngine faceEngine = StaticObj.FACE_ENGINE_POOL.borrowObject();
        StaticObj.FACE_ENGINE_POOL.returnObject(faceEngine);
    }

}
